package com.example.demo1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo1.common.BtException;
import com.example.demo1.dao.ProjectDAO;
import com.example.demo1.entity.Project;
import com.example.demo1.service.ProjectService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    ProjectDAO projectDAO;
    public void addProject(Project project){
        if(StringUtils.isNullOrEmpty(project.getProjectname())){
            BtException.throwException("项目名称为空");
        }
        QueryWrapper<Project> wrapperProject = new QueryWrapper<>();
        wrapperProject.eq("projectname",project.getProjectname());
        if(projectDAO.selectCount(wrapperProject)!=0){
            BtException.throwException("项目已存在");
        }
        project.setRecenttime(new Date());
        projectDAO.insert(project);
    }

    public void updateProject(Project project){
        QueryWrapper<Project> wrapperProject=new QueryWrapper<>();
        wrapperProject.eq("id",project.getId());
        project.setRecenttime(new Date());
        projectDAO.update(project,wrapperProject);
    }

    public void deleteProjrct(Project project){
        QueryWrapper<Project> wrapperProject=new QueryWrapper<>();
        wrapperProject.eq("id",project.getId());
        projectDAO.delete(wrapperProject);

    }

    public IPage<Project> getProjectList(int currentPage,int pageSize,Project project){
//        创建分页对象，current为当前页数，size为每天最大记录数
        Page<Project> pageProject=new Page<>(currentPage,pageSize);
        QueryWrapper<Project> wrapperProject=new QueryWrapper<>();
//        第一个参数为是否执行条件，为true则执行该条件
        wrapperProject.like(StringUtils.isNullOrEmpty(project.getProjectname()),"projectname",project.getProjectname());
//        根据项目id查询
        wrapperProject.eq(project.getId()>0,"id",project.getId());
//        id倒叙
        wrapperProject.orderByDesc("id");
//       调用分页查询方法，传入分页对象--pageProject，wrapperProject是构造条件对象
        return projectDAO.selectPage(pageProject,wrapperProject);
    }

    public List<Project> getProjectAll() {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        return projectDAO.selectList(queryWrapper);
    }
}
